package data;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;

public class Dictionary {
	private int size = 10000;
	private String path = null;
	private BufferedReader reader;
	
	public void setSize(int s) {
		size = s;
	}
	public void setPath(String file) {
		try {
			this.path = file;
				
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}
	
    public String getStringValueFor(int digital)  {
        return getStringValueFor(""+digital);
    }
    
    
    private Map hash=new HashMap();
    int HASH_SIZE = 100000;
    public String getStringValueFor(String key)  {
    	if (hash.get(key)!=null) return (String)hash.get(key);
    	if (hash.keySet().size()>=HASH_SIZE) {
    		Object firstKey  = hash.keySet().iterator().next();
    		hash.remove(firstKey);
    	}
    	String value = lookupStringValueFor(key);
    	hash.put(key,value);
    	return value;
    }
	public String lookupStringValueFor(String key)  {
		String result = null;
		 try {
			Reader f = new FileReader(path);
	        BufferedReader br = new BufferedReader(f);
	        reader = br;
			 String s = null;
			 String prevLine = null;
			 int cnt =0;
			 do {
				 
				 prevLine = s;
	             s=reader.readLine();
	             cnt++;
	            // Printer.println(cnt+" "+s);
	             if (s!=null) {
	            	 String[] sp = s.split("\t");
	            	 
	                 if (sp[0].equals(key)) {
	                	 if (sp.length==2) {
		                	 result =  sp[1];
	                	 } else {
	                		 result = "";
	                	 }
	                	 break;
	                 }
	             }
	         }while ((s!=null)&&(result==null));
			 br.close();
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
		return result;
	}
}
